using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class UnityScriptHandle : ScriptHandleBase
{
    /// <summary>
    /// 导出正常版代码
    /// </summary>
    public override void ExportScript(List<ExcelData> list, string path)
    {
        ExportClassScript(list, path);
        ExportLoaderScript(list, path, "/UnityLoaderTemplete2.txt", "/UnityResolvingTemplete.txt");
    }

    /// <summary>
    /// 导出压缩版代码
    /// </summary>
    public override void ExportCompressScript(List<ExcelData> list, string path)
    {
        ExportClassScript(list, path);
        ExportLoaderScript(list, path, "/UnityLoaderTemplete.txt", "/UnityResolvingTemplete.txt");
    }

    /// <summary>
    /// 导出Config类代码
    /// </summary>
    public override void ExportClassScript(List<ExcelData> list, string path)
    {
        for (int i = 0; i < list.Count; i++)
        {
            ExcelData data = list[i];

            string script_str = "namespace LFramework\n{\n";
            script_str += $"\tpublic struct {data.name}" + "\n\t{\n";
            for (int j = 0; j < data.types.Count; j++)
            {
                string note = list[i].notes[j].Replace("\n", "\n\t/// ");
                note = $"\n\t\t/// <summary>{note}</summary>\n";
                script_str += note + $"\t\tpublic {data.types[j]} {data.fields[j]};\n";
            }
            script_str += "\t}\n}";
            FileHandle.SaveFile(script_str, path + data.name + ".cs");
            LogTool.WriteLog($"导出代码: {data.name}.cs");
        }

    }

    public void ExportLoaderScript(List<ExcelData> list, string path, string pathLoader, string pathResolving)
    {
        string dicts = "";
        string resolvings = "";

        pathLoader = Application.streamingAssetsPath + pathLoader;
        pathResolving = Application.streamingAssetsPath + pathResolving;

        string dict_templete = "\t\tpublic Dictionary<{type}, {name}> {name}_Map = new Dictionary<{type}, {name}>();\n";
        string field_templete = "\n\t\t\t\t\t{field} = ConvertType<{type}>(fields[{index}]),";
        string field_templete_arr = "\n\t\t\t\t\t{field} = JsonConvert.DeserializeObject<{type}>(fields[{index}].ToString()),";
        StreamReader reader = new StreamReader(pathLoader);
        string loader = reader.ReadToEnd();
        reader.Close();

        reader = new StreamReader(pathResolving);
        string resolving_templete = reader.ReadToEnd();
        reader.Close();
        for (int i = 0; i < list.Count; i++)
        {
            ExcelData data = list[i];

            string fields = "";

            for (int j = 0; j < data.fields.Count; j++)
            {
                if (data.types[j].EndsWith("]"))
                {
                    fields += field_templete_arr.Replace("{field}", data.fields[j]).Replace("{type}", data.types[j]).Replace("{index}", j.ToString());
                }
                else if (data.types[j].EndsWith("$"))
                {
                    string _type = data.types[j].Replace("$", "");
                    fields += field_templete_arr.Replace("{field}", data.fields[j]).Replace("{type}", _type).Replace("{index}", j.ToString());
                }
                else
                {
                    fields += field_templete.Replace("{field}", data.fields[j]).Replace("{type}", data.types[j]).Replace("{index}", j.ToString());
                }
            }

            dicts += dict_templete.Replace("{name}", data.name).Replace("{type}", data.key_type);
            resolvings += resolving_templete.Replace("{fields}", fields).Replace("{name}", data.name).Replace("{key}", data.key);
        }

        loader = loader.Replace("{dictionary}", dicts).Replace("{resolving}", resolvings);
        FileHandle.SaveFile(loader, path + "JsonLoader.cs");
        LogTool.WriteLog($"导出代码: JsonLoader.cs");
    }
}
